﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CurriculumnManage.DBAccess;
using CurriculumnManage.Helper;

namespace CurriculumnManage
{
    public partial class frmMailSet : Form
    {
        private int iSyudaisyaID = 0;
        public frmMailSet()
        {
            InitializeComponent();
        }

        private void frmMailSet_Load(object sender, EventArgs e)
        {
            try
            {
                this.cmbGradeS.SelectedIndex = 0;
                string strGrade = this.cmbGradeS.Text;
                if (!String.IsNullOrEmpty(strGrade))
                    BindSubject(cmbSubjectS, int.Parse(strGrade));
            }
            catch (Exception ex)
            {
                CommonFunction.ShowError("frmMailSet.frmMailSet_Load", ex.Message);
            }
        }

        private void BindSubject(ComboBox cmb, int iGrade)
        {
            using (DBContext db = new DBContext())
            {
                SqlResult result = db.CallProcedure("uspWebCurriCulumnGetMailSubject", iGrade);
                if (result.DataSet.Tables.Count > 0 && result.DataSet.Tables[0].Rows.Count > 0)
                {
                    CommonFunction.BindCombox(result.DataSet.Tables[0], cmb);
                }
            }
        }



        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                UpdateData();
                ShowData();
            }
            catch (Exception ex)
            {
                CommonFunction.ShowError("frmMailSet.btnOK_Click", ex.Message);
            }
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            try
            {
                InitText();
                ShowData();
            }
            catch (Exception ex)
            {
                CommonFunction.ShowError("frmMailSet.btnSelect_Click", ex.Message);
            }
        }


        private void UpdateData()
        {
            int? grade;
            if (string.IsNullOrEmpty(this.cmbGrade.Text))
                grade= null;
            else
                grade = int.Parse(this.cmbGrade.Text);
            int? subject;
            if (string.IsNullOrEmpty(this.cmbSubject.SelectedValue.ToString()) || this.cmbSubject.SelectedValue.ToString()=="-1")
                subject = null;
            else
                subject = int.Parse(this.cmbSubject.SelectedValue.ToString());
            string teacher = string.IsNullOrEmpty(this.txtTeacher.Text)?null:this.txtTeacher.Text.Trim();
            string mail = string.IsNullOrEmpty(this.txtMail.Text) ? null : this.txtMail.Text.Trim();

            if (iSyudaisyaID == 0)
                return;
            using (DBContext db = new DBContext())
            {
                SqlResult result = db.CallProcedure("uspWebCurriCulumnInUpMailInfo", iSyudaisyaID, grade, subject, teacher, mail);
            }
        }


        private void InitText()
        {
            this.txtMail.Text = "";
            this.txtTeacher.Text = "";
            iSyudaisyaID = 0;
        }

        private void ShowData()
        {
            try
            {
                int? iGrade;
                int? iSubjectId;
                string teacher;
                string mail;
                if (string.IsNullOrEmpty(this.cmbGradeS.Text))
                    iGrade = null;
                else
                    iGrade = int.Parse(this.cmbGradeS.Text);
                if (cmbSubjectS.SelectedValue == null || string.IsNullOrEmpty(cmbSubjectS.SelectedValue.ToString()))
                    iSubjectId = null;
                else
                {
                    iSubjectId = int.Parse(cmbSubjectS.SelectedValue.ToString());
                    if (iSubjectId == -1)
                        iSubjectId = null;
                }

                teacher = string.IsNullOrEmpty(this.txtTeacherS.Text) ? null : this.txtTeacherS.Text;
                mail = string.IsNullOrEmpty(this.txtMailS.Text) ? null : this.txtMailS.Text;
                using (DBContext db = new DBContext())
                {
                    SqlResult result = db.CallProcedure("uspCurriCulumnGetMailInfo", iGrade, iSubjectId, teacher, mail);
                    if (result.DataSet.Tables.Count > 0 && result.DataSet.Tables[0].Rows.Count > 0)
                    {
                        this.gvMondai.DataSource = result.DataSet.Tables[0];
                    }
                }
            }
            catch (Exception ex)
            {
                CommonFunction.ShowError("frmMailSet.ShowData", ex.Message);
            }
        }

        private void gvMondai_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                string vGrade = "";
                string vSubject = "";
                string vTeacher = "";
                string vMail = "";
                int curRow;
                if (gvMondai.SelectedRows.Count < 1)
                    return;

                if (gvMondai.RowCount < 1)
                    return;

                curRow = gvMondai.CurrentCell.RowIndex;
                vGrade = gvMondai[1, curRow].Value.ToString();
                vSubject = gvMondai[2, curRow].Value.ToString();

                vTeacher = gvMondai[3, curRow].Value.ToString();
                vMail = gvMondai[4, curRow].Value.ToString();

                if (gvMondai[5, curRow] == null) return;

                iSyudaisyaID = int.Parse(gvMondai[5, curRow].Value.ToString());

                this.cmbGrade.Text = vGrade;
                if (!string.IsNullOrEmpty(vGrade))
                    BindSubject(cmbSubject, int.Parse(vGrade));
                cmbSubject.Text = vSubject;

                this.txtTeacher.Text = vTeacher;
                this.txtMail.Text = vMail;
            }
            catch (Exception ex)
            {
                CommonFunction.ShowError("frmMailSet.gvMondai_DoubleClick", ex.Message);
            }

        }

        private void gvMondai_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            try
            {
                if (e.RowIndex < 0) return;
                Rectangle rectangle = new Rectangle(e.RowBounds.Location, gvMondai[0, 0].Size);
                TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), gvMondai.RowHeadersDefaultCellStyle.Font, rectangle, gvMondai.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
            }
            catch (Exception ex)
            {
                CommonFunction.ShowError("frmMailSet.gvMondai_RowPostPaint", ex.Message);
            }
        }


        private void cmbGradeS_SelectedIndexChanged(object sender, EventArgs e)
        {
            string strGrade = cmbGradeS.Text;

            if (!string.IsNullOrEmpty(strGrade))
            {
                BindSubject(cmbSubjectS, int.Parse(strGrade));
            }
        }

        private void cmbGrade_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
